const webpack 			= require('webpack');
const path 				= require('path')
const htmlWebpackPlugin = require('html-webpack-plugin');
const miniCssExtractPlugin = require('mini-css-extract-plugin');

//获取本机时间
let newDate = new Date();
let nowDate = newDate.getFullYear()+'-'+(newDate.getMonth()+1)+'-'+newDate.getDate()+'-'+newDate.getHours()+'-'+newDate.getMinutes();

const env = process.env.NODE_ENV || 'production';

module.exports = {
	entry:{
		app:path.resolve(__dirname,'..','src/app.js')
	},
	output:{
		path:path.resolve(__dirname,'..','dist'),
		filename:'app.js',
	},
	module:{
		rules:[
			{
				test:/\.(jsx|js)$/,
				use:['babel-loader']
			},
			{
				test:/\.css$/,
				use: [
					{
						loader:miniCssExtractPlugin.loader,
					},
					{
						loader:'css-loader',
						options:{
							modules:true,	//是否开启css-modules
						}
					}
				]
			},
			{
				test:/\.(jpg|png|gif|svg|woff|woff2|eot|ttf|otf)$/,
				use:[
					{
						loader:'url-loader'
					}
				]
			}
		]
	},
	mode: env,
	plugins:[
		new htmlWebpackPlugin({
			filename:'index.html',
			template:path.resolve(__dirname,'..','src/index.html'),
			inject:true
		}),
		new miniCssExtractPlugin({
            filename: 'css/[name]_'+ nowDate +'.css',
        }),
		new webpack.HotModuleReplacementPlugin(),		//热更新配置
	],
	resolve:{
		alias:{
			"@":path.resolve(__dirname,'..','src'),		//@符号代指src文件夹目录
		},
		extensions: ['.js','.jsx','.css','.sass','.less','.json'],	//引入文件无后缀时，从左向右依次匹配
	},
	devServer: {
		contentBase: false,
		hot: true,
		port: 5000,
	}
}